#ifndef page_H
#define page_H

#include <QGraphicsItem>

class PageControl;
class Document;
class BGSource;

/**
 * A single page within a Document.
 * 
 * Can be drawn on and has a background.
 */
class Page : public QObject, public QGraphicsItem
{
    Q_OBJECT

public:
    /**
     * Creates a new page.
     * 
     * @param parent The Document the Page lives in.
     */
    Page(Document *parent);

    /**
     * Create a new page.
     * 
     * @param parent The Document the Page lives in.
     * @param background The BGSource that provides that background information for the page.
     */
    Page(Document *parent, BGSource* background);

    /**
     * Specifies the location and size of the Page within the QGraphicsScene.
     * 
     * Reimplented as this is a QGraphicsItem.
     */
    QRectF boundingRect() const;

    /**
     * Return a QRectF in which the user can draw on the page.
     * Usually the boundingRect minus the PageControl element.
     */
    QRectF drawingRect() const;

    /**
     * Paints this Page to the QGraphicsScene.
     * Might not be needed as its children are drawn automatically.
     * 
     * Reimplented as this is a QGraphicsItem.
     */
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

    /**
     * The size of this page in scene units.
     * This changes depending on the document's scaling.
     * Is used by boundingRect() to report the size.
     */
    QSizeF sceneSize() const;

    /**
     * Return the real size of this page.
     */
    QSizeF realSize() const { return m_realSize; }

    /**
     * The item type number used by the QGraphics framework.
     * Used by Document::pageAt to distinguish pages from other objects in the scene.
     */
    enum { Type = UserType + 1 };

    /**
     * Override the return function to return this implementation's type.
     */
    int type() const {return Type;}

    /**
     * Override the implementation of QGraphicsItem::setScale() to prevent the
     * PageControl from scaling.
     */
    void setScale(qreal oldScale, qreal newScale, qreal scaleFactor);

    /**
     * Return a deepCopy of this page.
     * Used by duplicate.
     */
    Page* deepCopy();

protected:
    /**
     * Initializes the page. This is actually constructor work, but I'm too stupid to
     * call one constructor from another one and so I extracted this init method
     * instead of repeating code.
     */
    void init(Document *parent, BGSource* background);

    /**
     * The Document which the pages lives in.
     */
    Document *m_parentDocument;

    /**
     * The BGSource providing the background of this page.
     */
    BGSource *m_bgsource;

    /**
     * The current size of this page as it is drawn in the scene.
     */
    QSizeF m_realSize;

    /**
     * The rendered pixmap of this page's background.
     */
    QGraphicsPixmapItem *m_bgPixmap;

    /**
     * The page control that is used for this page.
     */
    PageControl *m_pageControl;

public slots:
    /**
     * Called when the background for this page is ready and can be rendered
     * as a QGraphicsPixmapItem.
     */
    void render(QPixmap*);

    /**
     * Add a page after this one.
     * Called by the PageControl.
     */
    void addPageAfter();

    /**
     * Remove this page.
     * Called by the PageControl.
     */
    void remove();

    /**
     * Duplicate this page and insert it after this one.
     * Called by the PageControl.
     */
    void duplicate();

    /**
     * Recalculate the position of this page's children.
     * Called after the size of the page has changed.
     */
    void repositionChildren();

    /**
     * Move this page up and replace it with the page before it.
     */
    void moveUp();

    /**
     * Move this page down and replace it with the next page.
     */
    void moveDown();
};

#endif
